package com.yhg.qxmall.cartorder.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yhg.qxmall.cartorder.feign.FrontMemberService;
import com.yhg.qxmall.cartorder.utils.DateUtil;
import com.yhg.qxmall.cartorder.vo.CommentVo;
import com.yhg.qxmall.cartorder.vo.MemberInfoTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yhg.qxmall.cartorder.entity.FmsCommentEntity;
import com.yhg.qxmall.cartorder.service.FmsCommentService;
import com.yhg.common.utils.PageUtils;
import com.yhg.common.utils.R;



/**
 * 
 *
 * @author 凌月青缃
 * @email 13@qxmall.com
 * @date 2020-12-22 14:08:53
 */
@RestController
@RequestMapping("cartorder/comment")
public class FrontCommentController {
    @Autowired
    private FmsCommentService commentService;
    @Autowired
    FrontMemberService memberService;

    //用户对具体的sku评论
    @PostMapping("/addComment")
    public R addComment(@RequestBody CommentVo commentVo){
        //远程服务
        MemberInfoTo userInfo = memberService.getUserInfo(commentVo.getMemberId());
        FmsCommentEntity commentEntity = new FmsCommentEntity();

        commentEntity.setOrderId(commentVo.getOrderId());
        commentEntity.setMemberId(commentVo.getMemberId());
        commentEntity.setSkuId(commentVo.getSkuId());
        commentEntity.setMenberName(userInfo.getUsername());
        commentEntity.setMemberHead(userInfo.getHeader());
        commentEntity.setCreateTime(new Date());
        commentEntity.setCommentText(commentVo.getCommentText());

        boolean b = commentService.save(commentEntity);
        if (b){
            return R.ok().put("msg","评价成功");
        }else return R.error().put("msg","服务器繁忙，请稍后重试！");
    }


    @GetMapping("/getCommentList")
    public R getCommentList(@RequestParam Long skuId){
        List<FmsCommentEntity> skuComment = commentService.list(new QueryWrapper<FmsCommentEntity>().eq("sku_id", skuId));
        return R.ok().put("data",skuComment);

    }






    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = commentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		FmsCommentEntity fmsComment = commentService.getById(id);

        return R.ok().put("fmsComment", fmsComment);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody FmsCommentEntity fmsComment){
        commentService.save(fmsComment);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody FmsCommentEntity fmsComment){
        commentService.updateById(fmsComment);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        commentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
